﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PRODUCT.Product.database;
using ERP_RORZE_DLL.Product.Base;
using BaseDAO = ERP_RORZE_TOOL.DBAccess.BaseDAO;

namespace ERP_RORZE_PRODUCT.Product.Machine
{
    public partial class ctrlMachineList : DevExpress.XtraEditors.XtraUserControl
    {
        ClsMachineDAO clsmachineDAO = new ClsMachineDAO();
        DataTable dt;
        MachineTable b = new MachineTable();
        private LogUtils log;

        public ctrlMachineList()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        void Reload(object sender, EventArgs e)
        {
            dt = clsmachineDAO.GetAllMachine();
            dgvMachineList.DataSource = dt;
            gridView1.BestFitColumns();
        }
        private void dgvMachineList_Load(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnMachineCapacity_Click(object sender, EventArgs e)
        {
            frm_MachineCapacity frmCapacity = new frm_MachineCapacity();
            frmCapacity.Text = "Machine Capacity";
            frmCapacity.ShowDialog();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frm_MachineAdd frmAdd = new frm_MachineAdd();
            frmAdd.Reload += new EventHandler(Reload);
            frmAdd.ShowDialog();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            b.MachineID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MachineID").ToString();
            clsmachineDAO.DeleteMachine(b);

            //write Log
            log.Table = LogConstant.Table.Product.MachineList;
            log.Task = LogConstant.Task.Delete;
            log.Description = "MachineID: " + b.MachineID;
            log.WriteLog();

            Reload(sender, e);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                frm_MachineEdit frmEdit = new frm_MachineEdit();
                frmEdit.ID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MachineID").ToString();
                frmEdit.Reload += new EventHandler(Reload);
                frmEdit.ShowDialog();
            }
            catch
            {
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            btnAdd_Click(sender, e);
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            btnEdit_Click(sender, e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            btnDelete_Click(sender, e);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            clsConstantsFuction.ExportToExcel(gridView1, 1, true);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Alt)
            {
                if (e.KeyCode == Keys.E)
                {
                    if (gridView1.OptionsBehavior.Editable)
                    {
                        gridView1.OptionsBehavior.Editable = false;
                        colMGroup.OptionsColumn.AllowEdit = false;
                        colSGroup.OptionsColumn.AllowEdit = false;
                    }
                    else
                    {
                        gridView1.OptionsBehavior.Editable = true;
                        colID.OptionsColumn.AllowEdit = false;
                        colAccName.OptionsColumn.AllowEdit = false;
                        colCharge.OptionsColumn.AllowEdit = false;
                        colDensanCode.OptionsColumn.AllowEdit = false;
                        colDensanName.OptionsColumn.AllowEdit = false;
                        colDmachine.OptionsColumn.AllowEdit = false;
                        colMachineGroupID.OptionsColumn.AllowEdit = false;
                        colMachineID.OptionsColumn.AllowEdit = false;
                        colMachineName.OptionsColumn.AllowEdit = false;
                        colMaker.OptionsColumn.AllowEdit = false;
                        colMakerMachineNo.OptionsColumn.AllowEdit = false;
                        colMultiPalet.OptionsColumn.AllowEdit = false;
                        colRatio.OptionsColumn.AllowEdit = false;
                        colkakokikai.OptionsColumn.AllowEdit = false;

                        colMGroup.OptionsColumn.AllowEdit = true;
                        colSGroup.OptionsColumn.AllowEdit = true;
                    }
                }
            }
        }

        private void gridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = sender as GridView;


            //Kiểm tra đây là dòng dữ liệu mới hay cũ, nếu là mới thì mình insert
            if (view.IsNewItemRow(e.RowHandle))
            {
                //e.RowHandle trả về giá trị int là thứ tự của dòng hiện tại

            }
            //Cũ thì update
            else
            {
                string MGroup = (view.GetRowCellValue(e.RowHandle, view.Columns["MGroup"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["MGroup"]).ToString() + "'";
                string SGroup = (view.GetRowCellValue(e.RowHandle, view.Columns["SGroup"]).ToString() == "") ? "null" : "N'" + view.GetRowCellValue(e.RowHandle, view.Columns["SGroup"]).ToString() + "'";
                string id = (view.GetRowCellValue(e.RowHandle, view.Columns["ID"]).ToString() == "") ? "null" : view.GetRowCellValue(e.RowHandle, view.Columns["ID"]).ToString();

                string update = "update [222_Machine] set [MGroup]=" + MGroup
                    + ", SGroup = " + SGroup
                    + " where [ID] = " + id;
                BaseDAO.ExecuteNonQuery(update);

                //write Log
                log.Table = LogConstant.Table.Product.MachineList;
                log.Task = LogConstant.Task.Update;
                log.Description = "MGroup: " + MGroup + "; SGroup: " + SGroup + " where ID:" + id;
                log.WriteLog();


            }
        }
    }
}
